﻿using BaseFramework;
using LiteFramework.Enum;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Principal;

namespace LiteFramework.Model
{
    [Serializable]
    [ComVisible(true)]
    [Entity("SYS_User")]
    public sealed class User : PersistentEntity<User>, IPrincipal
    {
        public User()
            : this(null, "123456", UserType.Employee) { }
        public User(string userName, string initPassword = "123456", UserType userType = UserType.Member)
        {
            this.ID = -1;
            this.UserName = userName;
            this.InitPassword = initPassword;

            this.LoginTimes = 0;
            this.Active = true;
            this.IsOnline = false;
            this.LockPassword = false;
            this.CreatedAt = DateTime.Now;
            this.ClosingDate = new DateTime(2039, 12, 31);
        }

        #region Properties

        /// <summary>
        /// 主键
        /// </summary>
        [PrimaryKey(UnsavedValue = "-1")]
        [DisplayName("主键")]
        public int ID { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [Property]
        [DisplayName("用户名")]
        public string UserName { get; set; }
        /// <summary>
        /// 身份识别
        /// </summary>
        [Property]
        [DisplayName("身份识别")]
        private string SecurityPin
        {
            get { return this.GetValue("SecurityPin", null); }
            set
            {
                this.SetValue("SecurityPin", value);
                this.SetValue("AddSecurity", !String.IsNullOrEmpty(value));
            }
        }

        /// <summary>
        /// 备注
        /// </summary>
        [DisplayName("备注")]
        public string Remark
        {
            get { return this.GetValue("Remark", String.Empty); }
            set { this.SetValue("Remark", value); }
        }

        /// <summary>
        /// 最近活动时间
        /// </summary>
        [Property]
        [DisplayName("最近活动时间")]
        public DateTime LastActivityTime
        {
            get { return this.GetValue("LastActivityTime", DateTime.Now); }
            internal set { this.SetValue("LastActivityTime", value); }
        }

        /// <summary>
        /// 启用
        /// </summary>
        [Property]
        [DisplayName("启用")]
        public bool Active
        {
            get { return this.GetValue("Active", true); }
            set { this.SetValue("Active", value); }
        }

        /// <summary>
        /// 在线
        /// </summary>
        [DisplayName("在线")]
        public bool IsOnline
        {
            get { return this.GetValue("IsOnline", false); }
            private set { this.SetValue("IsOnline", value); }
        }

        /// <summary>
        /// 锁定
        /// </summary>
        [DisplayName("锁定")]
        [Property]
        public bool IsLockedOut
        {
            get { return this.GetValue("IsLockedOut", false); }
            private set { this.SetValue("IsLockedOut", value); }
        }

        public DateTime ClosingDate { get; set; }
        public int LoginTimes { get; set; }
        public DateTime CreatedAt { get; set; }
        public string Email { get; set; }
        public bool LockPassword { get; set; }

        public UserType UserType { get; set; }
        public LoginType LastLoginType { get; set; }
        public string RealName { get; set; }
        public string NickName { get; set; }
        public Gender Gender { get; set; }
        public string HeadImgURL { get; set; }
        public string Mobile { get; set; }

        #endregion

        /// <summary>
        /// 初始密码(用户自助注册时用，为空时，将使用默认密码新建用户)
        /// </summary>
        [DisplayName("初始密码")]
        public string InitPassword { get; set; }

        /// <summary>
        /// 剩余天数
        /// </summary>
        [DisplayName("剩余天数")]
        public int DaysRemain { get { return (this.ClosingDate - DateTime.Now).Days; } }

        /// <summary>
        /// 设置用户的OpenId
        /// </summary>
        /// <param name="type"></param>
        /// <param name="openId"></param>
        internal void setOpenId(LoginType type, string openId)
        {
            //switch (type)
            //{
            //    case LoginType.QQ:
            //        this.OpenApi_QQ_ID = openId;
            //        break;
            //    case LoginType.WeiXin:
            //        this.OpenApi_WeiXin_ID = openId;
            //        break;
            //    case LoginType.WeiBo:
            //        this.OpenApi_WeiBo_ID = openId;
            //        break;
            //}
        }

        public Organization Organization { get; set; }

        public IIdentity Identity { get { return (this.UserName != null) ? new GenericIdentity(this.UserName, "Custom") : null; } }

        public bool HasTask(string[] taskCodes) { return true; }

        public bool IsSystem { get; set; }
        public bool IsApprole { get { return this.UserName == "Application"; } }
        public Guid Company { get; set; }
        public Guid CompanyId { get; set; }

        public string CompanyName { get; private set; }
        public string OrganizationName { get; private set; }
        public string DepartmentName { get; private set; }

        public IList<int> OrganizationIDs { get; set; }
        public IList<Role> Roles { get; set; }

        public string ProviderUserKey { get; set; }

        public bool IsInRole(string role)
        {
            throw new NotImplementedException();
        }

        public static UserNameChoice ToChoice(User user) { return null; }

        public override string ToString()
        {
            if (!string.IsNullOrEmpty(this.RealName) && this.RealName != this.UserName)
                return string.Format("{0}({1})", this.UserName, this.RealName);
            else
                return this.UserName;
        }
    }
}